{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Merge Strings Alternately"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mergeAlternately"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #交替合并字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个字符串 <code>word1</code> 和 <code>word2</code> 。请你从 <code>word1</code> 开始，通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长，就将多出来的字母追加到合并后字符串的末尾。</p>\n",
    "\n",
    "<p>返回 <strong>合并后的字符串</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"abc\", word2 = \"pqr\"\n",
    "<strong>输出：</strong>\"apbqcr\"\n",
    "<strong>解释：</strong>字符串合并情况如下所示：\n",
    "word1：  a   b   c\n",
    "word2：    p   q   r\n",
    "合并后：  a p b q c r\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"ab\", word2 = \"pqrs\"\n",
    "<strong>输出：</strong>\"apbqrs\"\n",
    "<strong>解释：</strong>注意，word2 比 word1 长，\"rs\" 需要追加到合并后字符串的末尾。\n",
    "word1：  a   b \n",
    "word2：    p   q   r   s\n",
    "合并后：  a p b q   r   s\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word1 = \"abcd\", word2 = \"pq\"\n",
    "<strong>输出：</strong>\"apbqcd\"\n",
    "<strong>解释：</strong>注意，word1 比 word2 长，\"cd\" 需要追加到合并后字符串的末尾。\n",
    "word1：  a   b   c   d\n",
    "word2：    p   q \n",
    "合并后：  a p b q c   d\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= word1.length, word2.length <= 100</code></li>\n",
    "\t<li><code>word1</code> 和 <code>word2</code> 由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [merge-strings-alternately](https://leetcode.cn/problems/merge-strings-alternately/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [merge-strings-alternately](https://leetcode.cn/problems/merge-strings-alternately/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abc\"\\n\"pqr\"', '\"ab\"\\n\"pqrs\"', '\"abcd\"\\n\"pq\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        \"\"\"\n",
    "        len_word1 = len(word1)\n",
    "        len_word2 = len(word2)\n",
    "        curr_word1 = 0\n",
    "        curr_word2 = 0\n",
    "        rst = \"\"\n",
    "        while curr_word1 < len_word1 or curr_word2 < len_word2:\n",
    "            if curr_word1 < len_word1:\n",
    "                rst += word1[curr_word1]\n",
    "                curr_word1 += 1\n",
    "            else:\n",
    "                rst += word2[curr_word2:]\n",
    "                break\n",
    "            if curr_word2 < len_word2:\n",
    "                rst += word2[curr_word2]\n",
    "                curr_word2 += 1\n",
    "            else:\n",
    "                rst  += word1[curr_word1:]\n",
    "                break\n",
    "\n",
    "        return rst\n",
    "        \"\"\"\n",
    "        \n",
    "        \"\"\"\n",
    "        :type word1: str\n",
    "        :type word2: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        result = []\n",
    "        i = 0\n",
    "        while i < len(word1) or i < len(word2):\n",
    "            if i < len(word1):\n",
    "                result.append(word1[i])\n",
    "            else: \n",
    "                result.append(word2[i:])\n",
    "                break\n",
    "            if i < len(word2):\n",
    "                result.append(word2[i])\n",
    "            else:\n",
    "                result.append(word1[i+1:])\n",
    "                break\n",
    "            i += 1\n",
    "        return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        index = 0\n",
    "        res = ''\n",
    "        while index <len(word1) or index < len(word2):\n",
    "            if index < len(word1):\n",
    "                res += word1[index]\n",
    "            if index < len(word2):\n",
    "                res += word2[index]\n",
    "            index +=1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        return \"\".join([str(word1[i])+str(word2[i]) for i in range(min(len(word1),len(word2)))])+word1[min(len(word1),len(word2)):]+word2[min(len(word1),len(word2)):]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        index = 0\n",
    "        res = ''\n",
    "        while index < len(word1) or index < len(word2):\n",
    "            if index < len(word1):\n",
    "                res += word1[index]\n",
    "            if index < len(word2):\n",
    "                res += word2[index]\n",
    "            index += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        res = []\n",
    "        #print(res)\n",
    "        word1 = list(word1)\n",
    "        print(word1)\n",
    "        word2 = list(word2)\n",
    "        print(word2)\n",
    "        while len(word1)>0 and len(word2) >0:\n",
    "            w1 = word1.pop(0)\n",
    "            w2 = word2.pop(0)\n",
    "            res.append(w1)\n",
    "            res.append(w2)\n",
    "            if len(word1) == 0:\n",
    "                res += word2\n",
    "            if len(word2) == 0:\n",
    "                res += word1\n",
    "        print(res)\n",
    "        res = ''.join(res)\n",
    "        print(res)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        new_word = [-1 for _ in range(len(word1)+len(word2))]\n",
    "        for i in range(0, min(len(word1), len(word2))):\n",
    "            print(i)\n",
    "            new_word[i*2] = word1[i]\n",
    "            new_word[i*2+1] = word2[i]\n",
    "        res = word1 if len(word1) >= len(word2) else word2\n",
    "        res_n = max(len(word1), len(word2)) - min(len(word1), len(word2))\n",
    "        for i in range(-1, -res_n-1, -1):\n",
    "            new_word[i] = res[i]\n",
    "        return ''.join(new_word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        if len(word1) > len(word2):\n",
    "            mergeWord = ''\n",
    "            for i in range(len(word2)):\n",
    "                mergeWord += word1[i] + word2[i]\n",
    "            mergeWord += word1[len(word2):]\n",
    "        else:\n",
    "            mergeWord = ''\n",
    "            for i in range(len(word1)):\n",
    "                mergeWord += word1[i] + word2[i]\n",
    "            mergeWord += word2[len(word1):]\n",
    "        return mergeWord"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        m, n = len(word1), len(word2)\n",
    "        i = j = 0\n",
    "        ans = list()\n",
    "        while i < m and j < n:\n",
    "            ans.append(word1[i])\n",
    "            ans.append(word2[j])\n",
    "            i+=1\n",
    "            j+=1\n",
    "        ans = \"\".join(ans)\n",
    "        ans = ans + word1[i:] + word2[j:]\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mergeAlternately(self, word1: str, word2: str) -> str:\n",
    "        m ,n = len(word1),len(word2)\n",
    "        i,j =0,0\n",
    "        ans = list()\n",
    "        while i <m or j <n :\n",
    "            if i <m :\n",
    "                ans.append(word1[i])\n",
    "                i+=1\n",
    "            if j <n:\n",
    "                ans.append(word2[j])\n",
    "                j+=1\n",
    "        return \"\".join(ans)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
